/**
 * Created by chenyong on 2015/3/21.
 * 找粉丝全景javascript Api
 * 此处的Api将在krpano全景xml文件中调用
 */

//全局变量
var TeamSelect = 'neutral'

//检查登录
function p_check_login(success_action, failure_action){
    $.bigpixel.authCheck(
        function() {
            if (success_action !== undefined){
                pano().call(success_action);
            }
        },
        function() {
            if (failure_action !== undefined){
                pano().call(failure_action);
            }
        }
    );
}

//设置标注点类型
function setTeam(team)  {
    TeamSelect = team;
}

function _queryParamsFromPosition(pos){
    return '?atv='+pos.atv+'&ath='+pos.ath+'&fov='+pos.fov;
}


//qq登录
function p_qq_login(success_action, failure_action) {
    $.bigpixel.auth('qq', function () {
            if (success_action !== undefined) {
                pano().call(success_action);
            }
        },
        function () {
            if (failure_action !== undefined) {
                pano().call(failure_action);
            }
        });
}

//微信登陆\
function p_wechat_login(success_action, failure_action) {
    $.bigpixel.auth('wechat', function () {
            if (success_action !== undefined) {
                pano().call(success_action);
            }
        },
        function () {
            if (failure_action !== undefined) {
                pano().call(failure_action);
            }
        });
}

//读取当前用户信息，创建标注点
function p_create_my_tag(success_action, failure_action){

    $.bigpixel.authCheck(
        function() {
            var me = $.bigpixel.me();
            var tagicon = (TeamSelect == 'neutral' || !TeamSelect) ? 'me' : TeamSelect;

            create_tag(me.id, me.name, me.network, tagicon,
                function (hotspot_name){
                    if (success_action !== undefined){
                        pano().call(success_action + '('+ hotspot_name +')');
                    }
                }, function (){
                    if (failure_action !== undefined){
                        pano().call(failure_action);
                    }
                }
            );

        },
        function() {
            alert('没有登录');
        });


};

//创建标注点
function create_tag(id, name, network, tagicon, successcb, failurecb){
    var pos = pano_get_viewposition();
    var hotspotname = network+id;
    var type = 'me';
    var profile_url = 'http://wx.qq.com/' + id + '/picture';

    $.ajax({
        type: "POST",
        url: "/api/zhaofans/zhaofans-midi/tags",
        data: {
            atv: pos.atv,
            ath: pos.ath,
            fov: pos.fov,
            name: hotspotname,
            tagtype: type,
            network: network,
            networkid: id,
            profile_url: profile_url,
            ref: TeamSelect
        },
        dataType: "json",
        cache: false,
        success: function (data) {
            successcb(hotspotname);
        }
    });

}


